package csm.web.core.sitemap;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.jdom.Element;
import org.jdom.JDOMException;

import csm.common.Logger;
import csm.common.StringUtil;
import csm.common.xml.XmlDocument;

public final class SiteMap {
	private static SiteMap _instance = null;

	public synchronized static SiteMap getInstance(String configFilePath ){
		if( _instance == null)
			_instance = new SiteMap(configFilePath);
		return _instance;
	}

	public static SiteMap getInstance(){
		if( _instance == null)
			_instance = new SiteMap();
		return _instance;
	}


	private String configFile = "/WEB-INF/Sitemap.xml";
	private Map<String, String> TEMPLATE_MAP;
	private Map<String, PathInfo> PATHINFO_MAP;

	private String currentThemaName = "default";


	public SiteMap(  )
	{
		parse();
	}
	public SiteMap( String configFilePath  )
	{
		this.configFile = configFilePath;
		parse();
	}
	public synchronized void setCurrentThema( String newThemaName )
	{
		this.currentThemaName = newThemaName;
	}

	public String getCurrentThema()
	{
		return this.currentThemaName;
	}

	public String getTemplateFile(String templateName){
		return String.valueOf( TEMPLATE_MAP.get(templateName) );
	}

	private void parse()
	{
		Logger.info("Loading Sitemap Information form " + this.configFile);

		XmlDocument xml = null;
		try
		{
			xml = new XmlDocument(new File(this.configFile));
			TEMPLATE_MAP = new HashMap<String, String>();
			PATHINFO_MAP = new HashMap<String, PathInfo>();

			List<Element> templateLists = xml.selectNodes("/sitemap/templates/template");
			String templateName = "";
			String templateFile = "";

			for( Element el : templateLists)
			{
				templateName = el.getAttributeValue("name");

				if ( TEMPLATE_MAP.containsKey(templateName)) continue;

				templateFile = el.getText();
				TEMPLATE_MAP.put(templateName, templateFile);
			}

			List<Element> pathLists = xml.selectNodes("/sitemap/pathInfo/url");
			PathInfo path;

			for( Element el : pathLists)
			{
				path = new PathInfo();
				path.parse(el);

				if( PATHINFO_MAP.containsKey(path.getPath())) continue;

				templateFile = TEMPLATE_MAP.get(path.getTemplate());

				if( !StringUtil.isNullOrEmpty(templateFile))
					path.setTemplateFile(templateFile);

				PATHINFO_MAP.put(path.getPath(), path);
			}

		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public PathInfo getPathInfo(ServletContext context, HttpServletRequest request) throws Exception {
		String rootPath = !request.getContextPath().equals("/") ? request.getContextPath() + "/" : request.getContextPath();
		String url = request.getRequestURI().replace(rootPath, "");
		if (url.startsWith("/"))
			url = url.substring(request.getContextPath().length());

		if (url.startsWith("/"))
			url = url.substring(1);

		return PATHINFO_MAP.get(url);
	}
}
